namespace DimensionsHelper.Client.Navigation;

public class NavigationKeyBuilder
{

    public NavigationKeyBuilder(NavigationKeyBuilder? containingScope, string? currentScope)
    {
        if (containingScope != null)
        {
            _fullScope = containingScope._fullScope;
        }

        if (!string.IsNullOrEmpty(currentScope))
        {
            if (!string.IsNullOrEmpty(_fullScope))
            {
                _fullScope += ".";
            }
            
            _fullScope += currentScope;
        }
        else
        {
            _fullScope = "";
        }
    }
    
    
    private string? _cachedNavigationKey;

    private readonly string _fullScope;

    
    public string ServiceKey(string key)
    {
        if (string.IsNullOrEmpty(_cachedNavigationKey))
        {
            if (!string.IsNullOrEmpty(_fullScope))
            {
                _cachedNavigationKey = _fullScope + ".";
            }
            else
            {
                return _cachedNavigationKey = key;
            }
        }
        
        return _cachedNavigationKey += key;
    }
    
}